<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<html>
<head>
  <jsp:include page="templates/head.jsp" />
  <script type="text/javascript">
    $(document).ready(function() {
      $('#authorFnameInput').autocomplete({ source: '/book-app/book/ajax', 
    	  									select: function(event, ui) {
    	  										$(event.target).val(ui.item.label);
    	  										$('#authorFnameHidden').val(ui.item.value);
    	  										return false;
      										},
      										focus: function(event, ui) {
    	  										$(event.target).val(ui.item.label);
    	  										$('#authorFnameHidden').val(ui.item.value);
    	  										return false;
      										}
      });
    });
    $(document).ajaxStart(function(){ 
  	  $('#ajaxIndicator').show(); 
  	}).ajaxStop(function(){ 
  	  $('#ajaxIndicator').hide();
  	});
    </script>
</head>

<body>
  <jsp:include page="templates/header.jsp" />
  <div id="ContentBox">
    <c:if test="${not empty message}">
      <div class="ui-state-error" style="padding: 0.3em;">
        <span class="ui-icon ui-icon-alert" style="float: left; maring-right: .3em"></span>
        <c:out value="${message}" />
      </div>
    </c:if>
    <form:form id="formBook" action="save" method="post" modelAttribute="book">
      <form:label path="title">
        Tytuł <form:errors path="title" />
      </form:label>
      <br />
      <form:input path="title" cssStyle="width: 200px;" />
      <br />
      <form:label path="authors[0].fname">
        Autor <form:errors path="authors[0].fname" />
      </form:label>
      <br />
      <form:input id="authorFnameInput" path="authors[0].fname" cssStyle="width: 200px;"/>
      <form:hidden id="authorFnameHidden" path="authors[0].id"/>
      <br />
      <form:label path="description">
        Opis <form:errors path="description" />
      </form:label>
      <br />
      <form:textarea path="description" cssStyle="width: 200px;" />
      <br />
      <button type="submit">Zapisz</button>
    </form:form>
  </div>
  <jsp:include page="templates/footer.jsp" />
</body>
</html>
